-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScreen.cs
161 lines (153 loc) · 5.99 KB
/
Screen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
namespace My
{
using System;
using System.Drawing;
/// <summary>
/// 屏幕截图相关函数
/// </summary>
public sealed partial class Screen
{
/// <summary>
/// 获取屏幕截图(全屏区域的截图)
/// </summary>
/// <returns>结果图片(失败返回1*1个像素,#00000000透明色的图片)</returns>
public static Bitmap Image()
{
try
{
Rectangle bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap image = new Bitmap(bounds.Width, bounds.Height);
Graphics graphics = Graphics.FromImage(image);
graphics.CopyFromScreen(0, 0, 0, 0, bounds.Size);
graphics.Dispose();
return image;
}
catch (Exception ex)
{
return new Bitmap(1, 1);
}
}
/// <summary>
/// 获取屏幕截图(指定区域的截图)
/// </summary>
/// <param name="Area">指定区域(System.Drawing.Rectangle)</param>
/// <returns>结果图片(失败返回1*1个像素,#00000000透明色的图片)</returns>
public static Bitmap Image(Rectangle Area)
{
try
{
Bitmap image = new Bitmap(Area.Width, Area.Height);
Graphics graphics = Graphics.FromImage(image);
graphics.CopyFromScreen(Area.Left, Area.Top, 0, 0, Area.Size);
graphics.Dispose();
return image;
}
catch (Exception ex)
{
return new Bitmap(1, 1);
}
}
/// <summary>
/// 获取屏幕截图(全屏区域的缩略图)
/// </summary>
/// <param name="Scale">缩略比例(应大于0,且小于等于1)</param>
/// <returns>结果图片(失败返回1*1个像素,#00000000透明色的图片)</returns>
public static Bitmap ImageThumbnail(double Scale)
{
if (Scale <= 0 | Scale > 1)
{
return new Bitmap(1, 1);
}
try
{
Rectangle bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Bitmap image = new Bitmap(bounds.Width, bounds.Height);
Graphics graphics = Graphics.FromImage(image);
graphics.CopyFromScreen(0, 0, 0, 0, bounds.Size);
graphics.Dispose();
Bitmap thumbnail = (Bitmap)image.GetThumbnailImage((int)Math.Round((double)(bounds.Width * Scale)), (int)Math.Round((double)(bounds.Height * Scale)), null, new IntPtr(0));
image.Dispose();
return thumbnail;
}
catch (Exception ex)
{
return new Bitmap(1, 1);
}
}
/// <summary>
/// 获取屏幕截图(指定区域的缩略图)
/// </summary>
/// <param name="Area">指定区域(System.Drawing.Rectangle)</param>
/// <param name="Scale">缩略比例(应大于0,且小于等于1)</param>
/// <returns>结果图片(失败返回1*1个像素,#00000000透明色的图片)</returns>
public static Bitmap ImageThumbnail(Rectangle Area, double Scale)
{
if (Scale <= 0 | Scale > 1)
{
return new Bitmap(1, 1);
}
try
{
Bitmap image = new Bitmap(Area.Width, Area.Height);
Graphics graphics = Graphics.FromImage(image);
graphics.CopyFromScreen(Area.Left, Area.Top, 0, 0, Area.Size);
graphics.Dispose();
Bitmap thumbnail = (Bitmap)image.GetThumbnailImage((int)Math.Round((double)(Area.Width * Scale)), (int)Math.Round((double)(Area.Height * Scale)), null, new IntPtr(0));
image.Dispose();
return thumbnail;
}
catch (Exception ex)
{
return new Bitmap(1, 1);
}
}
/// <summary>
/// 获取屏幕区域
/// </summary>
/// <returns>结果区域值(System.Drawing.Rectangle)</returns>
public static Rectangle Area()
{
return System.Windows.Forms.Screen.PrimaryScreen.Bounds;
}
/// <summary>
/// 获取屏幕区域大小
/// </summary>
/// <returns>结果大小值(System.Drawing.Size)</returns>
public static System.Drawing.Size Size()
{
return System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
}
/// <summary>
/// 获取屏幕中心点坐标
/// </summary>
/// <returns>结果坐标值(System.Drawing.Point)</returns>
public static Point CenterPoint()
{
return new Point(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2);
}
/// <summary>
/// 获取屏幕工作区域
/// </summary>
/// <returns>结果区域值(System.Drawing.Rectangle)</returns>
public static Rectangle WorkingArea()
{
return System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
}
/// <summary>
/// 获取屏幕工作区域大小
/// </summary>
/// <returns>结果大小值(System.Drawing.Size)</returns>
public static System.Drawing.Size WorkingAreaSize()
{
return System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size;
}
/// <summary>
/// 获取屏幕工作区域中心点坐标
/// </summary>
/// <returns>结果坐标值(System.Drawing.Point)</returns>
public static Point WorkingAreaCenterPoint()
{
return new Point(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width / 2, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height / 2);
}
}
}